package utils

import (
	"bytes"
	"net/http"
	"io/ioutil"
	"encoding/json"
	"gitee.com/dayu1985/can_api/errors"
)

func POST(url string,param interface{}) (interface{},error){
	bytesData, err := json.Marshal(param)
	if err != nil {
		return nil,errors.JsonStrfyErr(err)
	}
	reader := bytes.NewReader(bytesData)
	request, err := http.NewRequest("POST", url, reader)
	if err != nil {
		return nil,errors.HttpRequestCreateErr(err)
	}
	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
	client := http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		return nil,errors.HttpResponseParseErr()
	}
	respBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil,errors.HttpGetDataErr()
	}
	//byte数组直接转成string，优化内存
	result := map[string]interface{}{}
	return result["Response"],json.Unmarshal(respBytes,&result)
}
